package com.capgemini.fs.app.ekms.service.implementations;

import java.util.List;
import java.util.Set;

import com.capgemini.fs.app.ekms.common.Constants;
import com.capgemini.fs.app.ekms.model.Group;
import com.capgemini.fs.app.ekms.model.User;
import com.capgemini.fs.app.ekms.persistence.PersistenceService;
import com.capgemini.fs.app.ekms.persistence.dao.GroupDAO;
import com.capgemini.fs.app.ekms.service.GroupService;
import com.capgemini.fs.app.ekms.service.UserService;

public class GroupServiceImpl implements GroupService {
    private PersistenceService persistenceService = null;
    private UserService userService = null;

    public void createGroup(Group g) throws Exception {
        getGroupDAO().save(g);
    }

    public void createGroup(List<Group> groups) {
        getGroupDAO().save(groups);
    }

    public void updateGroup(Group g) {
        getGroupDAO().evict(g);
        Group exsit = getGroupDAO().findByPrimaryKey(Group.class,g.getId());
        exsit.setUsers(g.getUsers());
        exsit.setStatus(g.getStatus());
        exsit.setLastUpdateDate(g.getLastUpdateDate());
        exsit.setCreateDate(g.getCreateDate());
        exsit.setCreator(g.getCreator());
        exsit.setDescription(g.getDescription());
        exsit.setGroupName(g.getGroupName());
        exsit.setLastModifier(g.getLastModifier());

        getGroupDAO().update(exsit);
    }

    public void updateGroup(List<Group> groups) {
        for (Group g : groups) {
            updateGroup(g);
        }
    }

    private GroupDAO getGroupDAO() {
        return this.getPersistenceService().getGroupDAO();
    }

    public PersistenceService getPersistenceService() {
        return persistenceService;
    }

    public void setPersistenceService(PersistenceService persistenceService) {
        this.persistenceService = persistenceService;
    }

    public boolean isExist(String name) {
        Group group=getGroupDAO().findGroupByGroupName(name);
        if ( group!= null) {
            return true;
        }  
            return false;
    }

    public Group findGoupByGroupId(Integer id) {

        return getGroupDAO().findByPrimaryKey(Group.class,id);
    }

    public Group findGroupByGroupName(String name) {
        return getGroupDAO().findGroupByGroupName(name);
    }

    public List<Group> findAllGroup() {
        return getGroupDAO().findAll(Group.class);
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public UserService getUserService() {
        return userService;
    }

    public List<Group> queryGroup(String groupName, Integer status) {
        return this.getGroupDAO().queryGroup(groupName, status);
    }

    public void activeGroup(Integer groupId,String lastModifier) {
        Group group = this.getGroupDAO().findByPrimaryKey(Group.class,groupId);
        group.setStatus(Constants.GROUP_STATUS_ACTIVE);
        group.setLastModifier(lastModifier);
        this.updateGroup(group);

    }

    public void activeGroupUser(Integer groupId,String lastModifier) {

        Group group = this.getGroupDAO().findByPrimaryKey(Group.class,groupId);

        Set<User> groupUser = group.getUsers();

        for (User user : groupUser) {
            user.setStatus(Constants.USER_STATUS_ACTIVE);
            user.setLastModifier(lastModifier);
            userService.updateUser(user);
            
        }

    }

    public void inactiveGroup(Integer groupId,String lastModifier) {
        Group group = this.getGroupDAO().findByPrimaryKey(Group.class,groupId);

        group.setStatus(Constants.USER_STATUS_INACTIVE);
        group.setLastModifier(lastModifier);
        this.updateGroup(group);
    }

    public void inactiveGroupUser(Integer groupId,String lastModifier) {
        Group group = this.getGroupDAO().findByPrimaryKey(Group.class,groupId);
        Set<User> groupUser = group.getUsers();
        for (User user : groupUser) {
            if (user.getGroups() == null || user.getGroups().size() == 1) {
                // When the user belongs to no group or belongs to only one
                // group.
                user.setLastModifier(lastModifier);
                user.setStatus(Constants.USER_STATUS_INACTIVE);
            } else {
                // When the user belongs to more than one groups.
                // Inactive the user only when all the groups are inactive.
                Set<Group> groupsOfUser = user.getGroups();
                boolean isAllInactived = true;
                for (Group userGroup : groupsOfUser) {
                    if (Constants.USER_STATUS_ACTIVE.equals(userGroup.getStatus())) {
                        isAllInactived = false;
                    }
                }
                if (isAllInactived) {
                    user.setLastModifier(lastModifier);
                    user.setStatus(Constants.USER_STATUS_INACTIVE);
                }
            }
            getUserService().updateUser(user);
        }

    }

}
